package com.example.recyclerview.test;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.example.recyclerview.R;

public class DeleteDialog extends DialogFragment {

    private OnClickListener onClickListener;

    public interface OnClickListener {
        void onClick(View view);
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    private View view;

    private String titleName;

    public static DeleteDialog getInstance(String titleName) {
        Bundle bundle = new Bundle();
        bundle.putString("titleName", titleName);
        DeleteDialog deleteDialog = new DeleteDialog();
        deleteDialog.setArguments(bundle);
        return deleteDialog;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            titleName = bundle.getString("titleName");
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.dialog_attr_del, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        TextView beta_cancel_button = view.findViewById(R.id.beta_cancel_button);
        TextView beta_confirm_button = view.findViewById(R.id.beta_confirm_button);
        TextView tv_title = view.findViewById(R.id.tv_title);

        tv_title.setText("确认删除该" + titleName + "？");

        beta_cancel_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        beta_confirm_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickListener != null) {
                    onClickListener.onClick(v);
                }
                dismiss();
            }
        });
    }
}
